package www.truongnha.com;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import TruongNhaLib.JSONParser;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;



public class ClassList extends ListActivity {
	 public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
        setContentView(R.layout.class_list);
        
        // getting intent data
        Intent in = getIntent();
        
        // Get JSON values from previous intent
        String name = in.getStringExtra("CLASS_NAME");
        String size = in.getStringExtra("CLASS_SIZE");
        String subject = in.getStringExtra("CLASS_SUBJECT");
        String mainteacher = in.getStringExtra("CLASS_MAINTEACHER");
       
        JSONParser jsonP = new JSONParser();
        JSONObject json = jsonP.getJSONFromUrl("http://10.0.2.2/android/classList/");
        ArrayList<HashMap<String, String>> studentsHashMap = new ArrayList<HashMap<String,String>>();
        
        try {
	        JSONArray students = json.getJSONArray("Danhsach");
	        int SIZE = students.length();
	        for(int i = 0; i < SIZE; i++){
	            HashMap<String,String> studentHashMap = new HashMap<String,String>();
	        	JSONObject student = students.getJSONObject(i);
	        	studentHashMap.put("name", student.getString("name"));
	        	Log.e("Test", student.getString("name"));
	        	studentsHashMap.add(studentHashMap);
	        }//End for.
        } catch (JSONException e) {
        	
            e.printStackTrace();
        }//End try 
        		
		/**
         * Updating parsed JSON data into ListView
         * */
        final ListAdapter adapter = new SimpleAdapter(this, studentsHashMap,
                R.layout.attend_item,
                new String[] { "name"}, new int[] {
                        R.id.std_name});
        setListAdapter(adapter);
        
        
		// selecting single ListView item 
		ListView lv = getListView();

		// Launching new screen on Selecting Single ListItem
		lv.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// getting values from selected ListItem				
				String name = ((TextView) view.findViewById(R.id.std_name)).getText().toString();
				Toast.makeText(getApplicationContext(), name, Toast.LENGTH_SHORT).show();
				AlertDialog alertDialog = new AlertDialog.Builder(ClassList.this).create();
				alertDialog.setTitle("Học sinh");
				alertDialog.setMessage(name);
				alertDialog.setIcon(R.drawable.alert_icon);
				alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
				      public void onClick(DialogInterface dialog, int which) {
				    	  dialog.cancel();
				    } });
				alertDialog.show();

			}
		});
        
        
        
	 }

	@Override
	public ListAdapter getListAdapter() {
		// TODO Auto-generated method stub
		Log.e("Test test","1");
		return super.getListAdapter();
		
	}

	@Override
	public ListView getListView() {
		// TODO Auto-generated method stub
		Log.e("Test test","2");
		return super.getListView();
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		Log.e("Test test","3");
		super.onListItemClick(l, v, position, id);
	}

}
